home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpat2-1.000 / xpat2-1 / xpat2-1.04 / src / Xlib-events.c < prev    next >
C/C++ Source or Header  |  1994-04-22  |  4KB  |  135 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    X patience version 2 -- module Xlib-events.c                 */
  5. /*                                         */
  6. /*    Additional event handlers for the Xlib interface.             */
  7. /*    written by Heiko Eissfeldt and Michael Bischoff                 */
  8. /*    see COPYRIGHT.xpat2 for Copyright details                 */
  9. /*                                         */
  10. /*                                         */
  11. /*****************************************************************************/
  12. #ifdef useXlib
  13. #include "X-pat.h"
  14.  
  15. /* helper functions for Xlib interface */
  16.  
  17. /* dispatcher for event on table */
  18. static void handle_table_event(XEvent *xev) {
  19.     switch(xev->xany.type) {
  20.      case ButtonPress:    button_press(&xev->xbutton);
  21.             break;
  22.      case ButtonRelease:button_release(&xev->xbutton);
  23.                         break;
  24.      case MotionNotify: mouse_motion(&xev->xmotion);
  25.                         break;
  26.      case KeyPress:    key_press(&xev->xkey);
  27.             break;
  28.      case Expose:    redraw_table(&xev->xexpose);
  29.             break;
  30.      case ConfigureNotify:resize_event(
  31.                    xev->xconfigure.width,
  32.                    xev->xconfigure.height);
  33.             break;
  34.      }
  35. }
  36.  
  37. void (*check_button_list(struct singlebutton *p, int num,
  38.     XButtonPressedEvent *xev))(void) {
  39.     while (num--) {
  40.     if (xev->x >= p->x && xev->x < p->x + p->w &&
  41.         xev->y >= p->y && xev->y < p->y + p->h) {
  42.         switch (xev->button) {
  43.         case Button1:
  44.         return p->left;
  45.         case Button2:
  46.         return p->middle;
  47.         case Button3:
  48.         return p->right;
  49.         }
  50.     }
  51.     ++p;
  52.     }
  53.     return NULL;
  54. }
  55.  
  56. /* events in confirm window */
  57. static void conf_button_press(XButtonPressedEvent *xev) {
  58.     void (*func)(void);
  59.     func = check_button_list(confirmbuttons, 2, xev);
  60.     if (func)
  61.     (*func)();
  62. }
  63.  
  64. static void redraw_confirm(XExposeEvent *xev) {
  65.     XFillRectangle(dpy, confirm.win, whitegc, 0, 0, confirm.w-1, confirm.h-1);
  66.     /* draw the text */
  67.     XDrawImageString(dpy, confirm.win, button.gc,
  68.         graphic.xgap, button.by+button.font->ascent, "Please confirm", 14);
  69.     XDrawImageString(dpy, confirm.win, button.gc, graphic.xgap,
  70.         2 * button.by+button.font->ascent + button.fontheight, confirm.text,
  71.         strlen(confirm.text));
  72.     redraw_buttons(0, 0, confirm.w, confirm.h, confirm.win, 2, confirmbuttons);
  73. }
  74.  
  75. static void handle_confirm_event(XEvent    *xev) {
  76.     switch(xev->xany.type) {
  77.     case ButtonPress:
  78.     conf_button_press(&xev->xbutton);
  79.     break;
  80.     case KeyPress:
  81.     key_press(&xev->xkey); /* go same window */
  82.     break;
  83.     }
  84. }
  85.  
  86.  
  87. static void redraw_finwin(XExposeEvent *xev) {
  88.     XFillRectangle(dpy, finished_win, whitegc, 0, 0,
  89.            FINISHED_W-1, FINISHED_H-1);
  90.     /* draw the text */
  91.     XDrawImageString(dpy, finished_win, button.gc,
  92.              (FINISHED_W - XTextWidth(button.font, "GONZO!", 6))/2,
  93.              FINISHED_H * 2 / 3, "GONZO!", 6);
  94.     XFlush(dpy);
  95. }
  96.  
  97. void handle_expose_event(XExposeEvent *xev) {
  98.     if (xev->window == confirm.win)
  99.         redraw_confirm(xev);
  100.     else if (xev->window == finished_win)
  101.     redraw_finwin(xev);
  102. }
  103.  
  104. void event_loop(void) {
  105.     XEvent xev;
  106.  
  107.     cmd_CancelSelection();
  108.     while (1) {
  109. #if 0
  110.     if (game.ind[FIRST_SLOT] == rules.cards_per_color * rules.numstacks
  111.         && !game.finished) {
  112.         /* all cards on the stacks and not yet notified */
  113.         game.finished = True;
  114.         XMoveWindow(dpy, finished_win, (graphic.width - FINISHED_W) / 2,
  115.             (graphic.height - FINISHED_H) / 2);
  116.         XMapWindow(dpy, finished_win);
  117.         redraw_finwin((XExposeEvent *)0);
  118.         show_message("You did it!");
  119.         write_log_file();
  120.         do_music(&xev);    /* play until next event */
  121.         XUnmapWindow(dpy, finished_win);
  122.     } else
  123. #endif
  124.         XNextEvent(dpy, &xev);
  125.  
  126.     if (xev.xany.window == table)
  127.         handle_table_event(&xev);
  128.     else if (xev.xany.type == Expose) /* expose events are treated separately */
  129.         handle_expose_event(&xev.xexpose);
  130.     else if (xev.xany.window == confirm.win)
  131.         handle_confirm_event(&xev);
  132.     }
  133. }
  134. #endif
  135.